//AT24C02.c
#include <REGX52.h>
#include "I2C.h"

//AT24C02的固定地址为1010.可配置地址在开发板上为0000
//所以SLAVE ADDRESS+W为0xA0，SLAVE ADDRESS+R为0xA1

#define AT24C02_ADDRESS 0xA0

/**
  * @brief  AT24C02写入一个字节,要延迟5ms,等待rom完成处理
  * @param  写入的地址WordAddress:0-255 ,要写入的数据Data
  * @retval 无
  */
void AT24C02_WriteByte(unsigned char WordAddress,Data){

	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS);	//从机地址
	I2C_ReceiveAck();	//接收应答
	I2C_SendByte(WordAddress);	//写入自地址
	I2C_ReceiveAck();
	I2C_SendByte(Data);		//写入数据
	I2C_ReceiveAck();
	I2C_Stop();
}

/**
  * @brief  从AT24C02读出一个字节
  * @param  0-255
  * @retval 无
  */
unsigned char AT24C02_ReadByte(unsigned char WordAddress){
	unsigned char Data;
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS);	//从机地址
	I2C_ReceiveAck();	//接收应答
	I2C_SendByte(WordAddress);	//写入自地址
	I2C_ReceiveAck();
	
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS|0x01);	//读取数据
	I2C_ReceiveAck();
	Data=I2C_ReceiveByte();
	I2C_SendAck(1);	//不应答,结束
	I2C_Stop();
	return Data;
}

